home *** CD-ROM | disk | FTP | other *** search
/ Univers Interactif 3 / INTERACTIF.BIN / mac / Planete.net / Internet Confirmés_Vrac / NetAgent.sea / src / filelock.c < prev    next >
Text File  |  1992-06-15  |  3KB  |  90 lines

  1. /*
  2. This is an unsupported product. It was developed as part of a study at Ostfold
  3. Regional College, Norway in the spring of 1992.
  4.  
  5. You use this software at your own risk. Neither the authors nor the Ostfold
  6. Regional College nor any other person or organization except yourself is
  7. responsible for any damage or loss of data derived from the use of this
  8. software. You may distribute this software freely as long as it is in its
  9. original form and all of its files are included. If you make any changes to any
  10. part of this software or the files distributed with it, please state so and do
  11. NOT distribute it under its original name.
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <sys/errno.h>
  16. #include <unistd.h>
  17. #include <sys/types.h>
  18. #include <fcntl.h>
  19.  
  20. extern int errno;
  21.  
  22. /*
  23. -----------------------------------------------------------------------------
  24. Function name: lock_file()
  25. -----------------------------------------------------------------------------
  26. Parameters:    int fd        File descriptor.
  27. Return value:    none
  28. Called by:    reg_user(), nytt(), nytt_ord(), new_words(), new_news(), 
  29.         hent_nye_innlegg(), do_innlegg, finn_stikkord(), do_user()
  30. Calls:        none
  31. Description:    Locks the file which is described by fd.
  32. -----------------------------------------------------------------------------
  33. */
  34.  
  35. void lock_file(fd)
  36. int fd;
  37. {
  38.     struct flock sf;
  39.  
  40.     sf.l_type = F_RDLCK|F_WRLCK;
  41.     sf.l_whence = SEEK_SET;
  42.     sf.l_start = 0;
  43.     sf.l_len = 0;
  44.     /* sf.l_pid = 0; */
  45.  
  46.     while(sf.l_type != F_UNLCK) {
  47.         if(fcntl(fd, F_GETLK, (int) &sf) == -1) {
  48.                     perror("fcntl F_GETLK");
  49.                     exit(1);
  50.             }
  51.     }
  52.  
  53.     sf.l_type = F_RDLCK|F_WRLCK;
  54.         sf.l_whence = SEEK_SET;
  55.         sf.l_start = 0;
  56.         sf.l_len = 0;
  57.     if(fcntl(fd, F_SETLK, (int) &sf) == -1) {
  58.         perror("fcntl(F_SETLK)");
  59.         exit(1);
  60.     }
  61. }
  62.  
  63. /*
  64. -----------------------------------------------------------------------------
  65. Function name: unlock_file()
  66. -----------------------------------------------------------------------------
  67. Parameters:    int fd        File descriptor.
  68. Return value:    none
  69. Called by:    reg_user(), nytt(), nytt_ord(), new_words(), new_news(),
  70.                 hent_nye_innlegg(), do_innlegg, finn_stikkord(), do_user()
  71. Calls:        none
  72. Description:    Unlocks the file described by fd.
  73. -----------------------------------------------------------------------------
  74. */
  75.  
  76. void unlock_file(fd)
  77. int fd;
  78. {
  79.     struct flock sf;
  80.  
  81.     sf.l_type = F_UNLCK;
  82.         sf.l_whence = SEEK_SET;
  83.         sf.l_start = 0;
  84.         sf.l_len = 0;
  85.         if(fcntl(fd, F_SETLK, (int) &sf) == -1) {
  86.                 perror("fcntl(u)(F_SETLK)");
  87.                 exit(1);
  88.         }
  89. }
  90.